package com.vipboot.platform.system.crud.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vipboot.framework.core.constant.Const;
import com.vipboot.framework.core.util.StringUtil;
import com.vipboot.platform.system.domain.bo.SysRoleBo;
import com.vipboot.platform.system.domain.entity.SysRole;
import com.vipboot.platform.system.crud.mapper.SysRoleMapper;
import com.vipboot.framework.mybatisplus.core.base.dao.PlusDaoImpl;
import com.vipboot.platform.system.domain.vo.SysRoleVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author daze
 * @since 2024-12-09
 */
@Service
public class SysRoleDao extends PlusDaoImpl<SysRoleMapper, SysRole> {

//    public LambdaQueryWrapper<SysRole> buildQueryWrapper(SysRoleBo bo) {
//        Map<String, Object> params = bo.getParams();
//        LambdaQueryWrapper<SysRole> lqw = Wrappers.lambdaQuery();
//        lqw.eq(ObjectUtil.isNotNull(bo.getRoleId()), SysRole::getRoleId, bo.getRoleId());
//        lqw.like(StringUtil.isNotBlank(bo.getRoleName()), SysRole::getRoleName, bo.getRoleName())
//                .eq(StringUtil.isNotBlank(bo.getStatus()), SysRole::getStatus, bo.getStatus())
//                .like(StringUtil.isNotBlank(bo.getRoleKey()), SysRole::getRoleKey, bo.getRoleKey())
//                .between(params.get("beginTime") != null && params.get("endTime") != null,
//                        SysRole::getCreateTime, params.get("beginTime"), params.get("endTime"))
//                .orderByAsc(SysRole::getSortNum)
//                .orderByDesc(SysRole::getCreateTime);
//        return lqw;
//    }

    public Wrapper<SysRole> buildQueryWrapper(SysRoleBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<SysRole> wrapper = Wrappers.query();
        wrapper.eq("r.delete_flag", Const.StatusNormal)
                .eq(ObjectUtil.isNotNull(bo.getRoleId()), "r.role_id", bo.getRoleId())
                .like(StringUtil.isNotBlank(bo.getRoleName()), "r.role_name", bo.getRoleName())
                .eq(StringUtil.isNotBlank(bo.getStatus()), "r.status", bo.getStatus())
                .like(StringUtil.isNotBlank(bo.getRoleKey()), "r.role_key", bo.getRoleKey())
                .between(params.get("beginTime") != null && params.get("endTime") != null,
                        "r.create_time", params.get("beginTime"), params.get("endTime"))
                .orderByAsc("r.sort_num").orderByAsc("r.create_time");
        return wrapper;
    }

    public List<SysRoleVo> list(SysRoleBo bo) {
        return getBaseMapper().selectRoleList(this.buildQueryWrapper(bo));
    }
}
